const config = require('./config/server');
const find = require('find');
const path = require('path');

async function findFiles() {
  return new Promise((resolve, reject) => {
    find.file(/\.js$/, path.join(__dirname, config.mockFilePath), (files) => {

      let apis = [];

      files.forEach(file => {
        let services = require(file);

        for (const key in services) {
          if (Object.hasOwnProperty.call(services, key)) {
            let [method, url] = key.split(' ');
            method = method.toLocaleLowerCase();
            apis.push({ method, url, service: services[key] });
          }
        }
      });

      resolve(apis);
    }).error(reject);
  });
};

module.exports = findFiles;